package com.ocom.wxapi.controller;


import com.alibaba.fastjson.JSON;
import com.ocom.common.entity.company.EntityCompanyInfo;
import com.ocom.common.request.wx.BindMachPageBindMachRequest;
import com.ocom.common.request.wx.BindMachPageOnLocdRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.response.wx.BindMachPageOnLoadResponse;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.BindMachVo;
import com.ocom.common.vo.MachVo;
import com.ocom.wxapi.client.AccessClientService;
import com.ocom.wxapi.client.CompanyClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@PreAuthorize("hasRole('ROLE_WX')")
@RequestMapping("/BindMachPage")
@RestController
public class BindMachPageController {

    @Autowired
    AccessClientService accessClientService;

    @Autowired
    CompanyClientService companyClientService;

    @PostMapping("/onLoad")
    public Result onLoad(@Valid BindMachPageOnLocdRequest request){
        BindMachPageOnLoadResponse bindMachPageOnLoadResponse=new BindMachPageOnLoadResponse();


        Map<String, String> pageFormat=new HashMap<>();
        pageFormat.put("pageTitle","门禁设备+");
        pageFormat.put("cmdAddCaption","添加门禁设备");
        pageFormat.put("cmdVerifyCaption","去验证");
        bindMachPageOnLoadResponse.setPageFormat(pageFormat);


        //1.companyName、endDate
        Result result=companyClientService.getCompanyInfoById(request.getCompanyId());
        if(result.getCode()==200){
            EntityCompanyInfo entityCompanyInfo= JSON.parseObject(JSON.toJSONString(result.getData()), EntityCompanyInfo.class);
            bindMachPageOnLoadResponse.setCompanyName(entityCompanyInfo.getComName());
        }

        Result result2=accessClientService.getMachineListOnLoad(request.getCompanyId());
        if(result2.getCode()==200){
            List<MachVo> machVos= JSON.parseObject(JSON.toJSONString(result2.getData()), List.class);
            bindMachPageOnLoadResponse.setMachList(machVos);
        }

        return ResultUtil.success(bindMachPageOnLoadResponse);
    }

    @PostMapping("/bindMach")
    public Result bindMach(@Valid BindMachPageBindMachRequest request){
        BindMachVo bindMachVo=new BindMachVo();
        bindMachVo.setComId(request.getCompanyId());
        bindMachVo.setPId(Integer.valueOf(String.valueOf(request.getPId())));
        bindMachVo.setDeviceNo(request.getMachNo());
        bindMachVo.setDeviceType(request.getDeviceType());
        bindMachVo.setDeviceName("ces");
        Result result=accessClientService.BindMach(bindMachVo);
        if(result.getCode()==200){
            return ResultUtil.info(200,"出厂号为"+request.getMachNo()+"的设备已登记，请尽快安装设备并验证。");
        }
        return result;
    }

}
